<template>
<!--工具条-->
  <div>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :data="userList">
        <el-form-item>
          <el-button type="primary" @click="handleAdd">注册用户</el-button>
          <el-dialog title="新增用户" v-model="addFormVisible" :visible.sync="addFormVisible">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
              <el-form-item label="用户名: " :label-width="formLabelWidth" class="adduserinfo" prop="username">
                <el-input v-model="addForm.username" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="密码: " :label-width="formLabelWidth" class="adduserinfo" prop="password">
                <el-input v-model="addForm.password" auto-complete="off" type="password"></el-input>
              </el-form-item>
              <el-form-item label="角色" :label-width="formLabelWidth" class="fl adduserinfo" prop="role">
                <el-select v-model="addForm.role" placeholder="请选择角色">
                  <el-option label="普通用户" value="1"></el-option>
                  <el-option label="管理员" value="10"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click.native="addFormVisible = false">取消</el-button>
              <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
          </el-dialog>
        </el-form-item>
      </el-form>
    </el-col>
    <el-table :data="userList" style="width: 100%" border>
      <!-- <el-table-column
        prop="_id"
        label="id"
        width="260">
      </el-table-column> -->
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="role"
        label="角色"
        width="180">
      </el-table-column>
      <el-table-column label="操作" width="150" v-loading="listLoading">
        <template slot-scope="scope">
          <el-button size="small" @click="openUserInfo(scope.$index)" >编辑</el-button>

          <el-button type="danger" size="small" @click="delUserInfo(scope.$index)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="查看/编辑" :visible.sync="dialogFormVisible">
      <el-form>
        <!-- <el-form-item label="id: " :label-width="formLabelWidth">
          <el-input  v-model="userId" auto-complete="off" :disabled="disabled"></el-input>
        </el-form-item> -->
        <el-form-item label="用户名: " :label-width="formLabelWidth">
          <el-input  :value='userName' auto-complete="off" :disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="角色: " :label-width="formLabelWidth" class="fl">
          <el-input  :value='userRole' auto-complete="off" :disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="旧密码: " :label-width="formLabelWidth" class="fl">
          <el-input  v-model='ruleForm.oldpasswd' type="password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码: " :label-width="formLabelWidth" class="fl">
          <el-input  v-model="ruleForm.newpasswd" type="password" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUserInfo()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import { Message } from 'element-ui'
import qs from 'qs'
export default {
  data () {
    return {
      masg: '管理员列表',
      userList: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      listLoading: false,
      userId: '',
      userName: '',
      userRole: '',
      currentIndex: '',
      disabled: true,
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      addForm: {
        username: '',
        password: '',
        role: ''
      },
      ruleForm: {
        oldpasswd: '',
        newpasswd: ''
      },
      rules: {
        passwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  async mounted () {
    let res = await axios.get('/admin')
    if (res.data.errno === 0) {
      this.userList = res.data.data
    }
  },
  methods: {
    // 用户列表
    async openUserInfo (index) {
      this.currentIndex = index
      this.dialogFormVisible = true
      let res = await axios.get('/admin/' + this.userList[index]._id)
      this.userId = (res.data.data._id)
      this.userName = (res.data.data.username)
      this.userRole = (res.data.data.role)
    },
    // 修改用户密码
    async updateUserInfo () {
      if (Number.isInteger(this.currentIndex)) {
        let res = await axios.put('/admin/' + this.userList[this.currentIndex]._id, qs.stringify({password: this.ruleForm.oldpasswd, new_password: this.ruleForm.newpasswd}))
        console.log(res)
        if (res.data.errno === 0) {
          Message.success({message: res.data.data})
        } else {
          Message.error({message: res.data.errmsg})
        }
        this.dialogFormVisible = false
      }
    },
    // 删除用户
    async delUserInfo (index) {
      await this.$confirm('确认删除?', '提示', {type: 'warning'})
      let res = await axios.delete('/admin/' + this.userList[index]._id)
      if (res.data.errno === 0) {
        Message.success({message: '删除成功!'})
        this.userList.splice(index, 1)
      }
    },
    // 显示新增界面
    handleAdd: function () {
      this.addFormVisible = true
      this.addForm = {
        username: '',
        password: '',
        role: ''
      }
    },
    // 新增
    async addSubmit () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {username: this.addForm.username, password: this.addForm.password, role: this.addForm.role}
          let res = await axios.post('/admin', qs.stringify(data))
          if (res.data.errno === 0) {
            data._id = res.data.data
            data.password = '***'
            this.userList.push(data)
            this.addFormVisible = false
            Message.success({message: '添加用户成功!'})
          } else {
            Message.error({message: res.data.errmsg})
          }
        }
      })
    },
    // 分页
    async handleCurrentChange (val) {
      let res = await axios.get('/admin?page=' + val)
      this.userList = res.data.data.data
    }
  }
}
</script>

<style  lang="sass">
.el-table__header-wrapper
  .el-table__header
    width: 100% !important
    table-layout: initial
.el-table__body
  width: 100% !important
.toolbar
  text-align: left
.el-table .cell
  text-align: center
.el-form .adduserinfo
  margin-bottom: 30px
  width: 100%
  margin-right: 0
  .el-form-item__content
    margin-left: 20px !important
</style>
